package cn.wolfcode.trip.base.service.impl;


import cn.wolfcode.trip.base.domain.StrategyAttention;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.mapper.StrategyAttentionMapper;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;

import cn.wolfcode.trip.base.service.IStrategyCommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 *@Discription 攻略评论管理

 *@Params

 *@Return

 *@Authod  黄照伟

 *@CreationTime  2019/3/11 14:45

 *@change

 */
@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {
    @Autowired
    private StrategyCommentMapper commentMapper;
    @Autowired
    private StrategyAttentionMapper attentionMapper;


    //分页查询
    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<StrategyComment> list = commentMapper.queryForList(qo);
        return new PageInfo(list);
    }
    //新增或保存评论
    @Override
    public void saveOrUpdate(StrategyComment strategyComment) {
        if (strategyComment.getId() == null) {
            commentMapper.insert(strategyComment);
        } else {
            commentMapper.updateByPrimaryKey(strategyComment);
        }
    }
    //查询出所有评论,并且分页
    @Override
    public PageInfo queryForApp(StrategyCommentQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<StrategyComment> list = commentMapper.queryFoListApp(qo);
        for (StrategyComment comment : list) {
            String[] imgUrlsArr = comment.getImgUrls().split(";");
            comment.setImgUrlsArr(imgUrlsArr);
        }
        return new PageInfo(list);
    }
    //通过攻略id查询出单个指定评论
    @Override
    public StrategyComment getStrategyCommentById(Long id) {
        StrategyComment comment = commentMapper.selectStrategyCommentById(id);
        return comment;
    }
    //保存评论,同时新增标签表中的关系
    @Override
    public Long save(StrategyComment strategyComment) {
        strategyComment.setCreateTime(new Date());
        commentMapper.insert(strategyComment);
        Long[] tagIds = strategyComment.getTagIds();
        for (Long tagId : tagIds) {
            commentMapper.insertId(tagId, strategyComment.getId());
        }

        return strategyComment.getId();
    }
    //查询出关注数量
    @Override
    public int getAssistCount(Long id) {
        return commentMapper.selectAssistCount(id);
    }
    //查询出所有攻略的评论
    @Override
    public int getStrategyCommentCount(Long userId) {
        return commentMapper.selectStrategyCommentCount(userId);
    }
    //通过用户id查询出所有评论以及数量
    @Override
    public Map<String, Object> getStrategyCommentsByUserId(Long id) {
        HashMap<String, Object> map = new HashMap<>();
        List<StrategyComment> commentList = commentMapper.selectStrategyCommentByUserId(id);
        int commentCount = commentMapper.selectStrategyCommentCount(id);
        map.put("commentList", commentList);
        map.put("commentCount", commentCount);
        for (StrategyComment comment : commentList) {
            String[] imgUrlArr = comment.getImgUrls().split(";");
            for (int i = 0; i < imgUrlArr.length; i++) {
                comment.setImgUrls(imgUrlArr[0]);
            }
        }
        return map;
    }
    //增加标签和评论的中间表关系
    @Override
    public void insertId(Long tagId, Long commentId) {
        commentMapper.insertId(tagId,commentId);
    }

    //重置用户的
    @Override
    public void resetCount(Long userId) {
        commentMapper.resetCount(userId);
    }
    //找到所有主评论
    @Override
    public List<StrategyComment> getAllStrategyComments() {
        return commentMapper.selectAllSubString();
    }
    //新增点赞关系
    @Override
    public void insertCountRelation() {
        commentMapper.updateCountRelation();
    }
    //查询所有关注信息
    @Override
    public StrategyAttention getCollect(StrategyAttention attention) {
        StrategyAttention collect = attentionMapper.selectCollect(attention);
        return attentionMapper.selectCollect(collect);
    }
    //查看是否收藏
    @Override
    public boolean iselectCollect(StrategyAttention attention) {
        StrategyAttention collect = attentionMapper.selectCollect(attention);
        //如果没有数据,则插入一条数据
        if (collect == null) {
            attentionMapper.insert(attention);
            return true;
        //如果查到数据则,将存在数据删除
        } else {

            /*attentionMapper.deleteByPrimaryKey(attention.getUserId());*/
            attentionMapper.deleteCollect(attention);
            return false;
        }
    }

    @Override
    public void deleteCollect(StrategyAttention attention) {
        attentionMapper.deleteCollect(attention);
    }

    @Override
    public List<StrategyComment> getUserIdByCommentId(Long commentId) {
        List<StrategyComment> comments = commentMapper.getUserIdByCommentId(commentId);
        return comments;
    }

    @Override
    public int selectLikeCountByCommentId(StrategyAttention attention) {
        int count = attentionMapper.selectLikeCountByCommentId(attention);
        return count;
    }

    @Override
    public void deleteChose(Long commentId, Long userId) {
        attentionMapper.deleteChose(commentId, userId);
    }

    @Override
    public void insertChose(Long commentId, Long userId) {
        attentionMapper.insertRelation(commentId, userId);
    }


    @Override
    public PageInfo strategyComment(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        return new PageInfo(commentMapper.selectForList(qo));
    }
}
